package com.dsshare.controller;

import com.dsshare.entity.ResponseModel;
import com.dsshare.util.GsonUtil;
import com.dsshare.util.RSAUtils;
import com.dsshare.util.RestUtil;
import com.dsshare.util.SignUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import static org.apache.commons.lang.math.RandomUtils.nextInt;

/**
 * Created by Simon on 2018/1/22.
 * Description:
 */
@Controller()
@RequestMapping(value = "/api")
public class SignController {

    @RequestMapping("/index")
    public String index(){
        return "apiDesc";
    }

    final String TYPE_TEST = "test";
    final String TYPE_DEV = "dev";

    @Value("#{keyproperties['privateKeyTest']}")
    String privateKeyTest ;
    @Value("#{keyproperties['privateKeyDev']}")
    String privateKeyDev;

    @Value("#{keyproperties['channelTest']}")
    String channelTest;
    @Value("#{keyproperties['channelDev']}")
    String channelDev;

    @Value("#{keyproperties['requestTokenUrlTest']}")
    String requestTokenUrlTest;
    @Value("#{keyproperties['requestTokenUrlDev']}")
    String requestTokenUrlDev;


    @RequestMapping(value = "/rsa.do",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String sign(@RequestParam("type") String type) throws Exception {
        String resultString;
        String chanelId;
        String privateKey;
        String requestTokenUrl;
        try {
            if (TYPE_TEST.equals(type)){
                chanelId = channelTest;
                privateKey = privateKeyTest;
                requestTokenUrl = requestTokenUrlTest;
            } else if (TYPE_DEV.equals(type)){
                chanelId = channelDev;
                privateKey = privateKeyDev;
                requestTokenUrl = requestTokenUrlDev;
            } else {
                chanelId = channelTest;
                privateKey = privateKeyTest;
                requestTokenUrl = requestTokenUrlTest;
            }
            int random = nextInt();
            long times = new Date().getTime();
            String data = chanelId + ";" + random + ";" + times;
            String sign = SignUtil.privateKeySign(data,privateKey);
            String query = "channel=" + chanelId + "&random=" + random + "&timestamp=" + times + "&sign=" + sign;

            RestUtil restUtil = new RestUtil();
            resultString = restUtil.load(requestTokenUrl,query);

//            ResponseModel responseModel = GsonUtil.fromJson(resultString, ResponseModel.class);
//            return responseModel.getAccessToken();

        } catch (Exception e) {
            resultString = e.getMessage();
            System.out.print(e.getMessage());
//            return resultString;
        }
        return resultString;
    }

}
